package com.zang.blogz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.constant.ResultConstant;
import com.zang.blogz.entity.Article;
import com.zang.blogz.model.input.ro.ArticleReviseRO;
import com.zang.blogz.model.output.ArticleSearchVO;
import com.zang.blogz.model.output.ImportArticleRO;
import com.zang.blogz.model.output.admin.AdminArchiveVO;
import com.zang.blogz.model.output.ArticleDetailsVO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.ArticleService;
import com.zang.blogz.strategy.context.ArticleImportStrategyContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 请我吃早饭
 * @since 2022/10/13
 */
@RestController
@RequestMapping("/blogz/article")
@Api(tags = "文章控制器模块")
public class ArticleController {


    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleImportStrategyContext articleImportStrategyContext;

    /**
     * @param id 文章id
     * @return {@link Result}<{@link ArticleDetailsVO}>
     */
    @GetMapping("/queryById")
    @ApiOperation(value = "根据id访问文章内容", notes = "根据id访问文章内容")
    public Result<ArticleDetailsVO> blogDetail(@RequestParam("id") Integer id) {

        return articleService.getBlogDetail(id);

    }

    /**
     * @param pageNo 后台文章页
     * @param pageSize 页大小
     * @return {@link Result}<{@link IPage}<{@link AdminArchiveVO}>>
     */
    @GetMapping("/admin")
    @ApiOperation(value = "获取后台文章列表", notes = "获取后台文章列表")
    public Result<IPage<AdminArchiveVO>> getAdminArchiveVoList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return articleService.getAdminArchiveVoList(pageNo, pageSize);
    }

    /**
     * @param articleId 文章id
     * @return {@link Result}<{@link String}>
     */
    @PostMapping("/admin/top")
    @ApiOperation(value = "更改文章置顶状态", notes = "更改文章置顶状态")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> updateTopStatus(@RequestParam(name = "articleId") Integer articleId) {
        return articleService.updateTopStatus(articleId);

    }

    /**
     * @param articleReviseRo 文章内容
     * @return {@link Result}<{@link String}>
     */
    @PostMapping("/admin/reviseArticle")
    @ApiOperation(value = "修改文章内容", notes = "修改文章内容")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> reviseOrSaveArticle(@RequestBody ArticleReviseRO articleReviseRo) {
        return articleService.reviseOrSaveArticle(articleReviseRo);
    }

    /**
     * @param articleId 文章id
     * @return {@link Result}<{@link String}>
     */
    @PostMapping("/login/saveArticleLike")
    @ApiOperation(value = "点赞文章", notes = "点赞文章")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> saveArticleLike(@RequestParam(name = "articleId") Integer articleId) {
        return articleService.saveArticleLike(articleId);
    }

    /**
     * @param keywords 搜索内容
     * @return {@link Result}<{@link List}<{@link ArticleSearchVO}>>
     */
    @ApiOperation(value = "全星球搜索")
    @GetMapping("/search")
    public Result<List<ArticleSearchVO>> listArticlesBySearch(@RequestParam("keywords") String keywords){

        return articleService.listArticlesBySearch(keywords);
    }

    /**
     * @param articleIds 文章id
     * @return {@link Result}<{@link List}<{@link String}>>
     */
    @ApiOperation(value = "导出文章")
    @ApiImplicitParam(name = "articleIdList", value = "文章id", required = true)
    @PostMapping("/admin/articles/export")
    public Result<List<String>> exportArticles(@RequestBody List<Integer> articleIds){

        return Result.ok(articleService.exportArticles(articleIds));
    }

    @ApiOperation(value = "导入文章")
    @PostMapping("/admin/articles/import")
    public Result<ArticleReviseRO> importArticles(@RequestParam("file") MultipartFile multipartFile,
                                                  ImportArticleRO importArticleRO ){

       return Result.ok(ResultConstant.FileMessage.FILE_IMPORT_S,articleImportStrategyContext.importArticles(multipartFile, importArticleRO)) ;
    }






}
